package com.citcc.business.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.citcc.business.bean.CdzStake;
import com.citcc.business.mapper.SysViewMapper;
import com.citcc.common.utils.StringUtils;
import com.citcc.common.utils.http.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SysViewService {


    private final SysViewMapper sysViewMapper;
    @Autowired
    public SysViewService(SysViewMapper sysViewMapper){
        this.sysViewMapper=sysViewMapper;
    }
    public Map<String,Object> getCountStake(String orgId){
        return sysViewMapper.getCountStake(orgId);
    }
    public Map<String,Object> getCountStation(String orgId,List<String> stakeCodes){
        return sysViewMapper.getCountStation(orgId,stakeCodes);
    }
    public Map<String,Object> getSumCharge(String orgId){
        return sysViewMapper.getSumCharge(orgId);
    }
    public Map<String,Object> getSumPayNumber(String orgId){
        return sysViewMapper.getSumPayNumber(orgId);
    }
    public Map<String,Object> getRealState(String type,String orgId,String stakeCodes){
        return sysViewMapper.getRealState(type,orgId,stakeCodes);
    }
    public Map<String,Object> getRealState(String type,String orgId,List<String> stakeCodes){
        return sysViewMapper.getRealState(type,orgId,stakeCodes);
    }
    public List<Map<String,Object>> getSumAll(String day,String orgId) {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date_str = format.format(date);
        String nextDay;
        Calendar calendar = Calendar.getInstance();//日历对象
        try {
            if(date_str.substring(0,date_str.lastIndexOf("-")).equals(day)){
                calendar.setTime(date);
                calendar.add(Calendar.DAY_OF_MONTH,+1);
            }else{
                calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(day+"-01 00:00:00"));//设置当前日期
                calendar.add(Calendar.MONTH, +1);//月份减一为-1，加一为1
            }
        } catch (ParseException e) {
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH,+1);
        }
        nextDay = format.format(calendar.getTime());
        return sysViewMapper.getSumAll(day,nextDay,orgId);
    }
    public Map<String,Object> getOnLine(String url){
        String result = HttpUtils.sendPost(url+"/v2/online",null);
        Map<String,Object> map = new HashMap<>();
        if(StringUtils.isNotEmpty(result)){
            JSONObject object = JSONObject.parseObject(result);
            if(object.get("code")!=null &&
                    "200".equals(object.get("code").toString())){
                map.put("STAKE_COUNT_ONLINE",object.get("data"));
            }
        }
        return map;
    }
    public String getOnLineStake(String url){
        String result = HttpUtils.sendPost(url+"/v2/onlineStake",null);
        String stakeCodes = "";
        if(StringUtils.isNotEmpty(result)){
            JSONObject object = JSONObject.parseObject(result);
            if(object.get("code")!=null &&
                    "200".equals(object.get("code").toString())){
                  JSONArray array = (JSONArray) object.get("data");
                  if(array !=null && !array.isEmpty()){
                      int index = 0;
                      for (Object str : array){
                          if(index == 0){
                              stakeCodes += "(";
                          }
                          stakeCodes += "'"+str+"'";
                          if(index<array.size()-1){
                              stakeCodes += ",";
                          }
                          if(index == array.size()-1){
                              stakeCodes += ")";
                          }
                          index++;
                      }
                  }
            }

        }
        return stakeCodes;
    }
    public List<String> getOnLineStakeList(String url){
        String result = HttpUtils.sendPost(url+"/v2/onlineStake",null);
        List<String> stakeCodes = new ArrayList<>();

        if(StringUtils.isNotEmpty(result)){
            JSONObject object = JSONObject.parseObject(result);
            if(object.get("code")!=null &&
                    "200".equals(object.get("code").toString())){
                JSONArray array = (JSONArray) object.get("data");
                if(array !=null && !array.isEmpty()){
                    for (Object str : array){
                        stakeCodes.add(str.toString());
                    }
                }
            }
        }
        if(stakeCodes.size() == 0){
            stakeCodes.add("isnull");
        }
        return stakeCodes;
    }
    public List<CdzStake> getStakeOnline(String orgId,List<String> stakeCodes){
        return sysViewMapper.getStakeOnline(orgId,stakeCodes);
    }
}
